﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ByteSite.Common;
using SqlSugar;
using System.Linq;

namespace ByteSite.Web.admin.form
{
    public partial class custom_list_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.custom_list().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("custom_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                SiteBind(); //绑定站点
                TableBind();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    //txtName.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=channel_name_validate");
                }
                ColumnBind(ddlTable.SelectedValue);
            }
        }

        #region 绑定站点=================================
        private void SiteBind()
        {
            BLL.sites bll = new BLL.sites();
            List<Entitys.dt_sites> list = bll.GetList(0, x => x.is_lock == 0, x => x.sort_id);

            this.ddlSiteId.Items.Clear();
            this.ddlSiteId.Items.Add(new ListItem("所有站点", ""));
            foreach (Entitys.dt_sites model in list)
            {
                this.ddlSiteId.Items.Add(new ListItem(model.title.ToString(), model.id.ToString()));
            }
        }
        #endregion

        #region 绑定数据表=================================
        private void TableBind()
        {
            BLL.database bll = new BLL.database();
            List<DbTableInfo> tableList = bll.TableList().OrderBy(x=>x.Name).ToList();
            List<DbTableInfo> viewList = bll.ViewList().OrderBy(x => x.Name).ToList();

            this.ddlTable.Items.Clear();
            foreach (DbTableInfo model in tableList)
            {
                this.ddlTable.Items.Add(new ListItem("table - " + model.Name.ToString(), model.Name.ToString()));
            }
            foreach (DbTableInfo model in viewList)
            {
                this.ddlTable.Items.Add(new ListItem("view - " + model.Name.ToString(), model.Name.ToString()));
            }
        }

        private void ColumnBind(string table)
        {
            BLL.database bll = new BLL.database();
            List<DbColumnInfo> columnList = bll.ColumnList(table);

            this.ddlKeyColumn.Items.Clear();
            this.ddlValueColumn.Items.Clear();
            foreach (DbColumnInfo model in columnList)
            {
                this.ddlKeyColumn.Items.Add(new ListItem(model.DbColumnName.ToString(), model.DbColumnName.ToString()));
                this.ddlValueColumn.Items.Add(new ListItem(model.DbColumnName.ToString(), model.DbColumnName.ToString()));
            }
        }
        #endregion


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Entitys.dt_custom_list model = new Entitys.dt_custom_list();
            model = new BLL.custom_list().GetModel(_id);

            ddlKeyColumn.SelectedValue = model.key;
            ddlTable.SelectedValue = model.table;
            txtTitle.Text = model.title;
            ddlValueColumn.SelectedValue = model.value;
            ddlSiteId.SelectedValue = model.site_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Entitys.dt_custom_list model = new Entitys.dt_custom_list();
            BLL.custom_list bll = new BLL.custom_list();

            model.add_time = model.update_time = DateTime.Now;
            model.add_user= model.update_user = GetAdminInfo().id;
            model.key = ddlKeyColumn.SelectedValue;
            model.table = ddlTable.SelectedValue;
            model.title = txtTitle.Text.Trim();
            model.value = ddlValueColumn.SelectedValue;
            model.category_id = 0;
            model.delete_flag = 0;
            model.site_id = Utils.ObjToInt(ddlSiteId.SelectedValue);
            model.sort_id = Utils.ObjToInt(txtSortId.Text.Trim());
            
            if (bll.Add(model) < 1)
            {
                return false;
            }

            AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加自定义列表" + model.title); //记录日志
            return true;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            Entitys.dt_custom_list model = new Entitys.dt_custom_list();
            BLL.custom_list bll = new BLL.custom_list();
            model.id = _id;
            model.add_time = model.update_time = DateTime.Now;
            model.add_user = model.update_user = GetAdminInfo().id;
            model.key = ddlKeyColumn.SelectedValue;
            model.table = ddlTable.SelectedValue;
            model.title = txtTitle.Text.Trim();
            model.value = ddlValueColumn.SelectedValue;
            model.category_id = 0;
            model.delete_flag = 0;
            model.site_id = Utils.ObjToInt(ddlSiteId.SelectedValue);
            model.sort_id = Utils.ObjToInt(txtSortId.Text.Trim());

            if (!bll.Update(model))
            {
                return false;
            }

            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改自定义列表" + model.title); //记录日志
            return true;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("custom_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改成功！", "custom_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                ChkAdminLevel("custom_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加成功！", "custom_list.aspx", "parent.loadMenuTree");
            }
        }

        protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            ColumnBind(ddlTable.SelectedValue);
        }
    }
}